﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using WomanMotion.WebApi.Api.Utility;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.UnifyResult;
using WomanMotion.WebApi.Model.Dtos;
using WomanMotion.WebApi.Model.Dtos.Contents;
using WomanMotion.WebApi.Model.Dtos.Match;
using WomanMotion.WebApi.Service;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Api.Controllers
{
    /// <summary>
    /// 文章管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [XSSFilter]
    [ApiController]
    [DescriptionName("文章管理", true, 80)]
    public class ContentsController : BaseController
    {
        private readonly IContentsService _contents;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="contents"></param>
        public ContentsController(ILogger<ContentsController> logger, IContentsService contents) : base(logger)
        {
            _contents = contents;
        }

        /// <summary>
        /// 创建文章
        /// </summary>
        /// <param name="dto">DTO</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ContentsVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("创建文章", true, "文章管理", "List", "ContentsList")]
        public async Task<IResultOutput> Created(CreatedContentsDTO dto)
        {
            var res = await _contents.CreatedAsync(dto);
            if (res != null)
            {
                return ResultOutput.Ok(res, "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 启用文章
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("启用文章")]
        public async Task<IResultOutput> Enable(PrimaryKeyDTO ids)
        {
            var res = await _contents.UpdateDisableAsync(DisableEnum.Enable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 禁用文章
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("禁用文章")]
        public async Task<IResultOutput> Disable(PrimaryKeyDTO ids)
        {
            var res = await _contents.UpdateDisableAsync(DisableEnum.Disable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 更新文章
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ContentsVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("更新文章")]
        public async Task<IResultOutput> Update(UpdateContentsDTO dto)
        {
            var res = await _contents.UpdateAsync(dto);
            if (res == null)
            {
                return ResultOutput.NotOk("操作失败");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取文章信息
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ContentsVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取文章信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetById(long id)
        {
            var res = await _contents.GetByIdAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取多表连接文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ContentsVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取多表连接文章信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetCommonById(long id)
        {
            var res = await _contents.GetByIDNavAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 分页获取文章数据
        /// </summary>
        /// <param name="pars">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<ContentsVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页获取文章数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetPageList(QueryParameterBase pars)
        {
            var res = await _contents.GetPageNavListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据文章标题搜索文章数据
        /// </summary>
        /// <param name="title">标题</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<ContentsSelectVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据文章标题搜索文章数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetLikeTitleList(string title)
        {
            var res = await _contents.GetListAsync(m => m.Title.Contains(title));

            if (res == null || res.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            var data = res.Select(x => new ContentsSelectVM() { Id = x.Id, label = x.Title });
            return ResultOutput.Ok(data, "操作成功");
        }
    }
}